package com.masterclient.app.modules.order.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.eoner.baselibrary.bean.goods.VipUpgradeBean;
import com.eoner.baselibrary.bean.order.ConfirmOrderPrice;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.managerlibrary.user.UserDataManager;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.masterclient.app.widget.UpgradeVipLayout;

public class SubmitOrderOtherInfoAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    private Activity mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount;
    private ConfirmOrderPrice data;
    private VipUpgradeBean vipUpgradeBean;
    private String point;
    private String buy_type;
    private String saleType;

    protected SubmitOrderOtherInfoAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams,VipUpgradeBean vipUpgradeBean, ConfirmOrderPrice data,String buy_type,String saleType) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.vipUpgradeBean = vipUpgradeBean;
        this.data = data;
        this.buy_type = buy_type;
        this.saleType = saleType;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        LinearLayout llAllPrice = holder.itemView.findViewById(R.id.ll_all_price);
        TextView tvAllPrice = holder.itemView.findViewById(R.id.tv_all_price);
        LinearLayout llFreight = holder.itemView.findViewById(R.id.ll_freight);
        TextView tvFreight = holder.itemView.findViewById(R.id.tv_freight);
        LinearLayout llPromotionDiscount = holder.itemView.findViewById(R.id.ll_promotion_discount);
        TextView tvPromotionDiscount = holder.itemView.findViewById(R.id.tv_promotion_discount);
        LinearLayout llDiscount = holder.itemView.findViewById(R.id.ll_discount);
        TextView tvDiscount = holder.itemView.findViewById(R.id.tv_discount);
        LinearLayout llRedPack = holder.itemView.findViewById(R.id.ll_red_pack);
        TextView tvRedPack = holder.itemView.findViewById(R.id.tv_red_pack);
        LinearLayout llCashCount = holder.itemView.findViewById(R.id.ll_cash_count);
        TextView tvCashCount = holder.itemView.findViewById(R.id.tv_cash_count);
        LinearLayout ll_gold_count = holder.itemView.findViewById(R.id.ll_gold_count);
        TextView tv_gold_count = holder.itemView.findViewById(R.id.tv_gold_count);
        LinearLayout ll_gold_pay = holder.itemView.findViewById(R.id.ll_gold_pay);
        TextView tv_gold_pay = holder.itemView.findViewById(R.id.tv_gold_pay);
        TextView tvFinalPrice = holder.itemView.findViewById(R.id.tv_final_price);
        UpgradeVipLayout upgradeVipLayout = holder.itemView.findViewById(R.id.upgrade_vip_layout);

        if (!TextUtils.isEmpty(saleType)&&"4".equals(saleType)){
            tvFinalPrice.setTextColor(Color.parseColor("#FF5000"));
        }
        if(vipUpgradeBean == null || TextUtils.isEmpty(vipUpgradeBean.getMessage())) {
            upgradeVipLayout.setVisibility(View.GONE);
        }else {
            //充值商品
            if ("3".equals(buy_type)){
                if (Integer.valueOf(UserDataManager.getInstance().getRoleLevel()) > 0){
                    //是会员
                    upgradeVipLayout.setVisibility(View.VISIBLE);
                    upgradeVipLayout.setData(vipUpgradeBean,true,false,"充话费确认订单页");
                }else {
                    //非会员
                    upgradeVipLayout.setVisibility(View.VISIBLE);
                    upgradeVipLayout.setData(vipUpgradeBean,true,true,"充话费确认订单页");
                }
            }else {
                //普通商品
                if (Integer.valueOf(UserDataManager.getInstance().getRoleLevel()) > 0){
                    //是会员
                    upgradeVipLayout.setVisibility(View.GONE);
                }else {
                    //非会员
                    upgradeVipLayout.setVisibility(View.VISIBLE);
                    upgradeVipLayout.setData(vipUpgradeBean,true,"确认订单页");
                }
            }
        }

        if (!TextUtils.isEmpty(data.getProducts_price_total()) && Float.valueOf(data.getProducts_price_total()) > 0) {
            tvAllPrice.setText("¥" + data.getProducts_price_total());
            llAllPrice.setVisibility(View.VISIBLE);
        }else {
            llAllPrice.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(data.getFreight_price()) && Float.valueOf(data.getFreight_price()) > 0) {
            tvFreight.setText("¥" + data.getFreight_price());
            llFreight.setVisibility(View.VISIBLE);
        }else {
            llFreight.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(data.getPromotion_deduction()) && Float.valueOf(data.getPromotion_deduction()) > 0) {
            tvPromotionDiscount.setText("-¥" + data.getPromotion_deduction());
            llPromotionDiscount.setVisibility(View.VISIBLE);
        } else {
            llPromotionDiscount.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(data.getCoupon_discount_amount()) && Float.valueOf(data.getCoupon_discount_amount()) > 0) {
            tvDiscount.setText("-¥" + data.getCoupon_discount_amount());
            llDiscount.setVisibility(View.VISIBLE);
        } else {
            llDiscount.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(data.getUse_bupiao()) && Float.valueOf(data.getUse_bupiao()) > 0) {
            tvRedPack.setText("-¥" + data.getUse_bupiao());
            llRedPack.setVisibility(View.VISIBLE);
        } else {
            llRedPack.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(data.getUse_balance()) && Float.valueOf(data.getUse_balance()) > 0) {
            tvCashCount.setText("-¥" + data.getUse_balance());
            llCashCount.setVisibility(View.VISIBLE);
        } else {
            llCashCount.setVisibility(View.GONE);
        }
        if(!TextUtils.isEmpty(data.getPay_integral_deduction())
                && Integer.parseInt(data.getPay_integral_deduction()) > 0){
            ll_gold_count.setVisibility(View.VISIBLE);
            tv_gold_count.setText("-¥" + data.getDeduction_amount());
        }else{
            ll_gold_count.setVisibility(View.GONE);
        }
        if (data.getPay_integral() != null) {
            point = data.getPay_integral();
        }
        if ((point != null && Float.valueOf(point) > 0)) {
            tv_gold_pay.setText(point);
            ll_gold_pay.setVisibility(View.VISIBLE);
        } else {
            ll_gold_pay.setVisibility(View.GONE);
        }

        Utils.setDin(tvFinalPrice,mContext);
        tvFinalPrice.setText("¥ " + data.getPay_price());
    }

    public void refreshUI(ConfirmOrderPrice confirmOrderPrice){
        this.data = confirmOrderPrice;
        notifyDataSetChanged();
    }

    public void refreshVipUpgradeUI(VipUpgradeBean vipUpgradeBean){
        this.vipUpgradeBean = vipUpgradeBean;
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 8;
    }
}
